<?php

namespace app\admin\controller;
use app\common\controller\Backend;
use think\DB;
/**
 *
 *
 * @icon fa fa-circle-o
 */
class Member extends Backend
{
    public function admin()
    {

        $array=session('admin');
        $where['id']=$array['id'];
        $uesr=DB::name('user')->where($where)->find();
        return $uesr;
    }
    public function index()
    {
        $mobile=$this->request->param('mobile');
        $where=array();
        if($mobile)
        {
            $where['m.mobile']=$mobile;
        }
        $user=$this->admin();
        if($user['id'] !=1)
        {
            $where['m.store_id']=$user['store_id'];
        }
        $list = Db::name('member')->alias('m')->join('level l','l.level = m.level')->where($where)->order('m.id desc')->field('m.*,l.name as level_name')->paginate(10);
        $list->appends(array('mobile'=>$mobile));
        $page = $list->render();
        $this->assign("page", $page);
        $this->assign('list',$list);
        return $this->fetch('member/index');

    }
    /*
     * 添加会员
     */
    public function add()
    {
        if($_POST)
        {
            $array=$this->request->param();
            $time = time();
            $year=date('Y',$time)+1;
            $month=date('m',$time);
            $days=date('d',$time);
            $e_time=$year.'-'.$month.'-'.$days;
            $array['e_time']=strtotime($e_time);
            $array['ctime']=$time;
            $state=DB::name('member')->insert($array);
            if($state)
            {
                return  $this->success('添加成功',url('member/index'));
            }else{
                return   $this->error('添加失败');
            }
        }else{
            $where['state']=1;
            $store=DB::name('store')->where($where)->select();
            $level=DB::name('level')->where($where)->select();
            $this->assign('level',$level);
            $this->assign('store',$store);
            return $this->fetch('member/add');
        }
    }
   public function detail(){
       if($_POST)
       {
           $where['id']=$_POST['id'];
           $date['level']=$_POST['level'];
           $date['mobile']=$_POST['mobile'];
           $date['uname']=$_POST['name'];
           $date['balance']=$_POST['balance'];
           $date['store_id']=$_POST['store_id'];
           $date['integral']=$_POST['integral'];
           $date['sex']=$_POST['sex'];
           $date['card_id']=$_POST['mobile'];
           $date['e_time']=date($_POST['e_time']);
           $member=DB::name('member')->where($where)->update($date);
           return $this->success($member);
       }else{
           $param = $this->request->param();
           $paramt=$param['id'];
           $member=DB::name('member')->where('id',$paramt)->find();
           $store=DB::name('store')->where(array('status'=>1))->select();
           $level=DB::name('level')->select();
           $this->assign('store',$store);
           $this->assign('member',$member);
           $this->assign('level',$level);
           return $this->fetch('detail');
       }
    }
    /*删除*/
    public  function delete(){
        $param = $this->request->param();
        $id=$param['id'];
        DB::name('menjin')->where('id',$id)->delete();
        $this->success('删除成功');
    }
}
